package org.zebra.common.core;

import org.zebra.common.exception.FailCode;
import org.zebra.common.exception.ResultException;
import org.zebra.common.exception.StatusCode;

/**
 * @author zhanghongbin
 */
public final class R {

    private R() {}

    public static void failed(FailCode failCode) {
        throw new ResultException(failCode.getCode(), failCode.getMsg());
    }

    public static void failed() {
        throw new ResultException(StatusCode.UNKNOWN.getCode(), StatusCode.UNKNOWN.getMsg());
    }

    public static void failed(String msg) {
        throw new ResultException(StatusCode.UNKNOWN.getCode(), msg);
    }

    /**
     * 成功后调用，返回的data为null
     *
     * @return HTTP请求，最外层响应对象
     */
    public static Result<?> ok() {
        return ok(null);
    }

    /**
     * 成功后调用，返回的data为一个对象
     *
     * @param <T>  泛型
     * @param data 数据
     * @return HTTP请求，最外层响应对象
     */
    public static <T> Result<T> ok(T data) {
        return new Result<T>()
                .toBuilder()
                        .code(StatusCode.SUCCESS.getCode())
                        .msg(StatusCode.SUCCESS.getMsg())
                        .data(data)
                        .build();
    }

    public static Result<?> error(String msg) {
        return new Result()
                .toBuilder().code(StatusCode.UNKNOWN.getCode()).msg(msg).build();
    }

    public static Result<?> error(FailCode failCode) {
        return new Result()
                .toBuilder().code(failCode.getCode()).msg(failCode.getMsg()).build();
    }
}
